package org.tinaelle.client.gui;

import org.tinaelle.client.gui.fieldSet.AdUserDataDetail;
import org.tinaelle.client.gui.grid.DataDetail;
import org.tinaelle.client.gui.grid.DataDetailItf;
import org.tinaelle.client.gui.grid.DataGrid2;
import org.tinaelle.client.gui.grid.DataUserDetail;
import org.tinaelle.client.gui.grid.DynaTableDataProvider;
import org.tinaelle.client.utils.TinaelCenterPanel;

import com.gwtext.client.core.UrlParam;
import com.gwtext.client.data.ArrayReader;
import com.gwtext.client.data.Store;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.layout.RowLayout;
import com.gwtext.client.widgets.layout.RowLayoutData;
import com.gwtextux.client.data.GWTProxy;

public class DataPanel<T> extends TinaelCenterPanel {
	private final DynaTableDataProvider<T>	dataProvider;
	private final DataDetailItf	           dataDetailProvider;

	@Override
	public String getHelpText() {
		return "<h2>Help Text....</h2>";
	}

	@Override
	public String getIntro() {
		return "<h2>Into</h2>";
	}

	public boolean showEvents() {
		return true;
	}

	@Override
	public Panel getViewPanel() {
		if ( panel == null ) {
			panel = new Panel();

			panel.setLayout( new RowLayout() );

			final Store store = new Store( ( GWTProxy ) dataProvider, new ArrayReader( dataProvider.getRecordDef() ),
			        true );
			store.setBaseParams( new UrlParam[] { new UrlParam( "paramName", "paramValue" ) } );

			if ( dataDetailProvider instanceof AdUserDataDetail ) {
				dataDetail = new DataUserDetail<T>( store, dataProvider, dataDetailProvider );
			} else {
				dataDetail = new DataDetail<T>( store, dataProvider, dataDetailProvider );
			}

			final DataGrid2<T> dataGrid2 = new DataGrid2<T>( store, dataProvider, dataDetail );
			panel.add( dataGrid2, new RowLayoutData( "50%" ) );
			panel.add( dataDetail, new RowLayoutData( "50%" ) );
		}
		return panel;
	}

	private DataDetail<T>	dataDetail;

	public DataPanel(final DynaTableDataProvider<T> dataProvider, final DataDetailItf dataDetailProvider) {
		this.dataProvider = dataProvider;
		this.dataDetailProvider = dataDetailProvider;
	}

}
